rates = read.table('收益率.csv', header=T, sep=',')

library('forecast')
SSEC = auto.arima(rates$SSEC, trace=T)
SCI = auto.arima(rates$SCI, trace=T)
FTSE100 = auto.arima(rates$FTSE100, trace=T)
DAX30 = auto.arima(rates$DAX30, trace=T)
CAC40 = auto.arima(rates$CAC40, trace=T)

print('ARIMA最优模型')
print(SSEC)
print(SCI)
print(FTSE100)
print(DAX30)
print(CAC40)

SSEC_Res = na.omit(SSEC$residuals)
SCI_Res = na.omit(SCI$residuals)
FTSE100_Res = na.omit(FTSE100$residuals)
DAX30_Res = na.omit(DAX30$residuals)
CAC40_Res = na.omit(CAC40$residuals)

# GARCH
print('开始GARCH')
library('fGarch')
#library('rugarch')
garch = function(seq)
{
  model = garchFit(~garch(1,1), data=seq, trace=F, cond.dist='std')
  return(model)
}
SSEC_GARCH = garch(SSEC_Res)
SCI_GARCH = garch(SCI_Res)
FTSE100_GARCH = garch(FTSE100_Res)
DAX30_GARCH = garch(DAX30_Res)
CAC40_GARCH = garch(CAC40_Res)

print('GARCH结果')
print(SSEC_GARCH)
print(SCI_GARCH)
print(FTSE100_GARCH)
print(DAX30_GARCH)
print(CAC40_GARCH)

SSEC_GARCH_Res = residuals(SSEC_GARCH)
SCI_GARCH_Res = residuals(SCI_GARCH)
FTSE100_GARCH_Res = residuals(FTSE100_GARCH)
DAX30_GARCH_Res = residuals(DAX30_GARCH)
CAC40_GARCH_Res = residuals(CAC40_GARCH)

library('tseries')
print('残差ADF检验')
print(adf.test(SSEC_GARCH_Res))
print(adf.test(SCI_GARCH_Res))
print(adf.test(FTSE100_GARCH_Res))
print(adf.test(DAX30_GARCH_Res))
print(adf.test(CAC40_GARCH_Res))
print('残差BDS检验')
print(bds.test(SSEC_GARCH_Res))
print(bds.test(SCI_GARCH_Res))
print(bds.test(FTSE100_GARCH_Res))
print(bds.test(DAX30_GARCH_Res))
print(bds.test(CAC40_GARCH_Res))
library('goftest')
print('残差CVM检验')
print(cvm.test(SSEC_GARCH_Res))
print(cvm.test(SCI_GARCH_Res))
print(cvm.test(FTSE100_GARCH_Res))
print(cvm.test(DAX30_GARCH_Res))
print(cvm.test(CAC40_GARCH_Res))

